from django.http import HttpResponse

from django.shortcuts import render, redirect

from user.forms import LoginForm
from user.models import CustomUser


# Create your views here.
from django.contrib.auth import login as auth_login

def login(request):
    form = LoginForm(request.POST or None)
    if request.method == 'POST' and form.is_valid():
        username = form.cleaned_data['username']
        user = CustomUser.objects.get(username=username)
        auth_login(request, user)          # 关键：写入 session
        return redirect('user:index')
    return render(request, 'user/login.html', {'form': form})



def register(request):
    return render(request, 'user/register.html')